//
//  THJMyFocusVC.m
//  THJXHW
//
//  Created by 河南德艺 on 17/4/12.
//  Copyright © 2017年 河南德艺. All rights reserved.
//

#import "THJMyFocusVC.h"
#import "THJMyFocusCell.h"
#import "ZBNetworking.h"
#import <MJRefresh.h>

@interface THJMyFocusVC ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView * _tableview;
}
@property(nonatomic,strong)NSMutableArray * dataArr;
@end

@implementation THJMyFocusVC
-(NSMutableArray*)dataArr{
    if(!_dataArr){
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    if(KIsiPhoneX){
        [self setXNavBarWithTitle:@"我的关注"];
    }else{
        [self setNavBarWithTitle:@"我的关注"];
    }
    [self getDataWithPage:1];
    [self setTableview];
}
-(void)getDataWithPage:(NSInteger)page{
    __weak typeof(self) weekself = self;
    NSString * urlstr = [NSString stringWithFormat:@"%@%@",THJUrlDomain,@"index.php?f=fav&r=fav"];
    NSLog(@"我的关注url-%@",urlstr);
    NSString * token = [[NSUserDefaults standardUserDefaults] objectForKey:@"token"];
    [MBProgressHUD showMessage:@"" toView:self.view];
    [ZBRequestManager requestWithConfig:^(ZBURLRequest *request){
        request.urlString=urlstr;
        request.apiType=ZBRequestTypeRefresh;
        [request setValue:token forHeaderField:@"token"];
    }  success:^(id responseObject,apiType type){
        NSDictionary * receiveData = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        [MBProgressHUD hideHUDForView:weekself.view animated:YES];
        if([receiveData[@"status"] isEqual:@"200"]){
            for (NSDictionary * dic  in receiveData[@"data"]) {
                [weekself.dataArr addObject:dic];
            }
            [_tableview reloadData];
        }else if(![receiveData[@"status"] isEqual:@"200"]){
            [weekself HUDShowMsg:receiveData[@"tip"] AfterDelayDimiss:1];
            [weekself pop];
        }
    } failed:^(NSError *error){
        [MBProgressHUD hideHUDForView:weekself.view animated:YES];
        NSLog(@"error-%@",error);
    }];
}
-(void)setTableview{
    _tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64) style:UITableViewStylePlain];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.tableFooterView = [UIView new];
    _tableview.showsVerticalScrollIndicator = NO;
    _tableview.rowHeight = 105;
    [_tableview registerNib:[UINib nibWithNibName:@"THJMyFocusCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    _tableview.backgroundColor = SingleColor(whiteColor);
    [self.view addSubview:_tableview];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary * dic = self.dataArr[indexPath.row];
    THJMyFocusCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.titleLab.text = dic[@"company"];
    cell.addressLab.text = dic[@"address"];
    [cell.imgV sd_setImageWithURL:[NSURL URLWithString:dic[@"logo"]] placeholderImage:kGetImage(@"占位图")];
    cell.productLab.text = [NSString stringWithFormat:@"主营：%@",dic[@"business"]];
    cell.favBtn.tag = indexPath.row + 100;
    [cell.favBtn addTarget:self action:@selector(addFav:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"dianjicell");
}
-(void)addFav:(UIButton*)btn{
    NSInteger index = btn.tag - 100;
    NSDictionary * dic = self.dataArr[index];
    NSString * itemid = dic[@"cid"];
    NSString * token = [[NSUserDefaults standardUserDefaults]objectForKey:@"token"];
    NSString * url = [NSString stringWithFormat:@"%@%@",THJUrlDomain,@"index.php?f=fav&r=fav&m=del"];
    [THJNetWork postDataFromUrl:url parameters:@{@"cid":itemid} Token:token callback:^(NSDictionary* receiveData, NSError *error) {
        [self HUDShowMsg:receiveData[@"tip"] AfterDelayDimiss:1];
    }];
    [self.dataArr removeObjectAtIndex:index];
    [_tableview reloadData];
}
@end
